<template>
  <div class="property-panel">
    <h4>节点属性</h4>
    <div v-if="!node" class="no-selection">
      <div class="no-selection-icon">📋</div>
      <div class="no-selection-text">请选择画布上的节点进行编辑</div>
    </div>
    
    <el-form v-else label-width="80px" :model="node" size="small" class="node-form">
      <el-form-item label="名称">
        <el-input v-model="node.name" placeholder="请输入节点名称" />
      </el-form-item>
      
      <el-form-item label="类型">
        <el-tag :type="getTypeColor(node.type)" size="small">{{ node.type }}</el-tag>
      </el-form-item>
      
      <el-form-item label="描述">
        <el-input 
          v-model="node.description" 
          type="textarea" 
          :rows="3"
          placeholder="请输入节点描述"
        />
      </el-form-item>
      
      <el-form-item label="负责人员">
        <el-input v-model="node.employee" placeholder="请输入负责人员" />
      </el-form-item>
      
      <el-form-item label="执行超时">
        <el-input v-model="node.timeout" placeholder="如：24h, 3d, 1w" />
      </el-form-item>
      
      <!-- 通知配置 - 根据节点类型和名称显示 -->
      <div v-if="showNotificationConfig" class="notification-section">
        <h5>通知配置</h5>
        
        <el-form-item label="通知对象">
          <el-select v-model="node.notifyTargets" multiple placeholder="请选择通知对象" style="width: 100%">
            <el-option label="买家" value="buyer" />
            <el-option label="卖家" value="seller" />
            <el-option label="客服" value="service" />
            <el-option label="财务" value="finance" />
            <el-option label="仓库管理员" value="warehouse" />
            <el-option label="物流员" value="logistics" />
            <el-option label="系统管理员" value="admin" />
          </el-select>
        </el-form-item>
        
        <el-form-item label="通知方式">
          <el-checkbox-group v-model="node.notifyMethods">
            <el-checkbox label="sms">短信</el-checkbox>
            <el-checkbox label="email">邮件</el-checkbox>
            <el-checkbox label="push">推送</el-checkbox>
            <el-checkbox label="wechat">微信</el-checkbox>
            <el-checkbox label="system">系统消息</el-checkbox>
            <el-checkbox label="qywx">企业微信</el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        
        <el-form-item label="通知时机">
          <el-select v-model="node.notifyTiming" placeholder="请选择通知时机" style="width: 100%">
            <el-option label="节点开始时" value="start" />
            <el-option label="节点完成时" value="complete" />
            <el-option label="节点失败时" value="error" />
            <el-option label="超时时" value="timeout" />
          </el-select>
        </el-form-item>
        
        <el-form-item label="通知内容">
          <el-input 
            v-model="node.remindTemplate" 
            type="textarea" 
            :rows="4"
            placeholder="请输入通知内容模板，可使用变量：{{订单号}}、{{用户名}}、{{状态}}等"
          />
        </el-form-item>
        
        <el-form-item label="失败重发">
          <el-switch v-model="node.repeatNotify" />
          <span style="margin-left: 8px; font-size: 12px; color: #666;">开启后失败时重复发送</span>
        </el-form-item>
      </div>
      
      <el-form-item label="执行条件">
        <div class="condition-editor">
          <el-input 
            v-model="node.condition" 
            type="textarea" 
            :rows="3"
            placeholder="请输入节点执行的前置条件（可选）&#10;示例：订单金额 > 1000 && 用户等级 >= 3"
          />
          
          <!-- 条件模板 -->
          <div class="condition-templates">
            <div class="template-title">常用条件模板：</div>
            <div class="template-list">
              <el-tag 
                v-for="template in conditionTemplates" 
                :key="template.key"
                size="small" 
                class="template-tag"
                @click="insertCondition(template.value)"
              >
                {{ template.label }}
              </el-tag>
            </div>
          </div>
          
          <!-- 变量选择器 -->
          <div class="variable-selector">
            <div class="variable-title">可用变量：</div>
            <div class="variable-list">
              <el-tag 
                v-for="variable in availableVariables" 
                :key="variable.key"
                size="small" 
                type="info"
                class="variable-tag"
                @click="insertVariable(variable.value)"
              >
                {{ variable.label }}
              </el-tag>
            </div>
          </div>
        </div>
      </el-form-item>
      
      <el-form-item label="错误处理">
        <el-select v-model="node.errorHandling" placeholder="请选择错误处理方式" style="width: 100%">
          <el-option label="重试" value="retry" />
          <el-option label="跳过" value="skip" />
          <el-option label="终止" value="stop" />
          <el-option label="通知管理员" value="notify" />
        </el-select>
      </el-form-item>
      
      <el-form-item label="重试次数">
        <el-input-number 
          v-model="node.retryCount" 
          :min="0" 
          :max="10"
          style="width: 100%"
        />
      </el-form-item>
      

      
      <!-- 操作按钮 -->
      <div class="form-actions">
        <el-button type="primary" size="small" @click="saveNode">保存</el-button>
        <el-button size="small" @click="resetNode">重置</el-button>
        <el-button type="danger" size="small" @click="deleteNode">删除节点</el-button>
      </div>
    </el-form>
  </div>
</template>

<script setup lang="ts">
import { ref, watch, computed } from 'vue'
import { ElMessage } from 'element-plus'

interface Node {
  id: string | number
  name: string
  type: string
  description?: string
  employee?: string
  timeout?: string
  remindTemplate?: string
  condition?: string
  errorHandling?: string
  retryCount?: number
  // 通知相关属性
  notifyTargets?: string[]
  notifyMethods?: string[]
  notifyTiming?: string
  repeatNotify?: boolean
}

const props = defineProps<{
  node: Node | null
}>()

const emit = defineEmits<{
  'update:node': [node: Node]
  'delete:node': [nodeId: string | number]
}>()

// 保存原始节点数据用于重置
const originalNode = ref<Node | null>(null)

// 监听节点变化，保存原始数据
watch(() => props.node, (newNode) => {
  if (newNode) {
    originalNode.value = JSON.parse(JSON.stringify(newNode))
  }
}, { immediate: true })

// 条件模板数据
const conditionTemplates = ref([
  { key: 'order_amount', label: '订单金额 > 1000', value: '订单金额 > 1000' },
  { key: 'user_level', label: '用户等级 >= 3', value: '用户等级 >= 3' },
  { key: 'stock_available', label: '库存数量 > 0', value: '库存数量 > 0' },
  { key: 'order_status', label: '订单状态 === "已确认"', value: '订单状态 === "已确认"' },
  { key: 'vip_user', label: '用户类型 === "VIP"', value: '用户类型 === "VIP"' },
  { key: 'workday', label: '工作日 === true', value: '工作日 === true' },
  { key: 'complex_condition', label: '复合条件', value: '(订单金额 > 1000 && 用户等级 >= 3) || 用户类型 === "VIP"' }
])

// 可用变量数据
const availableVariables = ref([
  { key: 'order_id', label: '订单号', value: '{{订单号}}' },
  { key: 'user_id', label: '用户ID', value: '{{用户ID}}' },
  { key: 'order_amount', label: '订单金额', value: '{{订单金额}}' },
  { key: 'user_level', label: '用户等级', value: '{{用户等级}}' },
  { key: 'order_status', label: '订单状态', value: '{{订单状态}}' },
  { key: 'stock_quantity', label: '库存数量', value: '{{库存数量}}' },
  { key: 'product_category', label: '商品分类', value: '{{商品分类}}' },
  { key: 'current_time', label: '当前时间', value: '{{当前时间}}' },
  { key: 'system_status', label: '系统状态', value: '{{系统状态}}' }
])

// 计算是否显示通知配置
const showNotificationConfig = computed(() => {
  if (!props.node) return false
  
  // 所有节点类型都支持通知配置，除了开始和结束节点
  if (props.node.type === '系统' && ['开始', '结束'].includes(props.node.name)) {
    return false
  }
  
  return true
})

function getTypeColor(type: string) {
  const colorMap: Record<string, string> = {
    '用户操作': 'primary',
    '财务': 'success',
    '仓库': 'warning',
    '物流': 'info',
    '系统': 'danger'
  }
  return colorMap[type] || 'default'
}

function saveNode() {
  if (props.node) {
    ElMessage.success('节点属性已保存')
    emit('update:node', props.node)
  }
}

function resetNode() {
  if (props.node && originalNode.value) {
    Object.assign(props.node, originalNode.value)
    ElMessage.info('节点属性已重置')
  }
}

function deleteNode() {
  if (props.node) {
    ElMessage.success('节点已删除')
    emit('delete:node', props.node.id)
  }
}

// 插入条件模板
function insertCondition(condition: string) {
  if (props.node) {
    props.node.condition = condition
    ElMessage.success('条件模板已插入')
  }
}

// 插入变量
function insertVariable(variable: string) {
  if (props.node) {
    const currentCondition = props.node.condition || ''
    props.node.condition = currentCondition + variable
    ElMessage.success('变量已插入')
  }
}
</script>

<style scoped>
.property-panel {
  width: 320px;
  background: #fff;
  border-left: 1px solid #eee;
  padding: 20px;
  min-height: 600px;
  overflow-y: auto;
}

.property-panel h4 {
  margin: 0 0 20px 0;
  color: #333;
  font-size: 16px;
  border-bottom: 2px solid #1890ff;
  padding-bottom: 8px;
}

.no-selection {
  text-align: center;
  padding: 40px 20px;
  color: #999;
}

.no-selection-icon {
  font-size: 48px;
  margin-bottom: 16px;
}

.no-selection-text {
  font-size: 14px;
}

.node-form {
  margin-top: 16px;
}

.notification-section {
  margin: 20px 0;
  padding: 16px;
  background: #f0f9ff;
  border-radius: 8px;
  border-left: 4px solid #1890ff;
}

.notification-section h5 {
  margin: 0 0 16px 0;
  color: #1890ff;
  font-size: 14px;
  font-weight: 600;
}

.form-actions {
  margin-top: 24px;
  padding-top: 16px;
  border-top: 1px solid #eee;
  display: flex;
  gap: 8px;
  justify-content: center;
}

:deep(.el-form-item__label) {
  font-weight: 500;
  color: #333;
}

:deep(.el-input__inner) {
  border-radius: 4px;
}

:deep(.el-textarea__inner) {
  border-radius: 4px;
}

:deep(.el-select) {
  width: 100%;
}

:deep(.el-checkbox-group) {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.condition-editor {
  margin-top: 8px;
}

.condition-templates,
.variable-selector {
  margin-top: 12px;
  padding: 8px;
  background: #f8f9fa;
  border-radius: 4px;
}

.template-title,
.variable-title {
  font-size: 12px;
  color: #666;
  margin-bottom: 8px;
  font-weight: 500;
}

.template-list,
.variable-list {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.template-tag,
.variable-tag {
  cursor: pointer;
  transition: all 0.2s;
}

.template-tag:hover {
  background: #1890ff;
  color: white;
}

.variable-tag:hover {
  background: #52c41a;
  color: white;
}
</style> 